package Buy

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/attr"
	"sanguo/node/common/com_type"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	table_Store "sanguo/node/table/excel/table/Store"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionBuy struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.BuyToC
	errcode cs_message.EnumType
}

func (this *transactionBuy) GetModuleName() string {
	return "Buy"
}

func (this *transactionBuy) GetReplyMsg() proto.Message {
	return &cs_message.BuyToC{}
}

func (this *transactionBuy) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionBuy) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionBuy) Begin() {

	this.resp = &cs_message.BuyToC{}
	msg := this.req.GetData().(*cs_message.BuyToS)
	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()

	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(role.GetUserID(), role.GetID(), "role is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infof("%s %d Call Buy id:%d count:%d", role.GetUserID(), role.GetID(), msg.GetID(), msg.GetCount())

	goods := table_Store.Table_.IDMap[msg.GetID()]
	if goods == nil {
		node_game.Infoln(role.GetUserID(), role.GetID(), "Buy fail: goods is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	gold, _ := role.GetRoleAttr().GetAttr(attr.Gold)
	diamond, _ := role.GetRoleAttr().GetAttr(attr.Diamond)
	if goods.CurrencyType == com_type.BuyCost_Gold {
		gold -= int64(goods.Price) * int64(msg.GetCount())
	} else if goods.CurrencyType == com_type.BuyCost_Diamond {
		diamond -= int64(goods.Price) * int64(msg.GetCount())
	}
	if gold < 0 || diamond < 0 {
		node_game.Infoln(role.GetUserID(), role.GetID(), "Buy fail : gold or diamond is not enough")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	switch goods.Type {
	case com_type.Buy_Item:
		item := map[int32]int32{
			goods.ItemID: msg.GetCount(),
		}
		role.AddItem(item)
		role.OnItemDirty()
	}

	role.GetRoleAttr().SetAttr(attr.Gold, gold)
	role.GetRoleAttr().SetAttr(attr.Diamond, diamond)

	node_game.Infoln(role.GetUserID(), role.GetID(), "Buy OK")
	this.errcode = cs_message.EnumType_OK
	this.user.EndTrans(this)
}

func (this *transactionBuy) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionBuy) Timeout() {
	this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_RETRY).Enum()
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.BuyToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionBuy{
			user: user,
			req:  msg,
		}
	})
}
